﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public static partial class Utility
{

    [Serializable]
    public class AnimationData
    {
        public AnimationCurve curve;
        public float time;
        public Vector4 param;
        public bool loop;
        public float delay;
    }

    public static class Animation
    {

        public static IEnumerator Play(AnimationData data, Action<float> onUpdate)
        {
            var st = Time.time;
            while(Time.time - st < data.delay)
            {
                yield return null;
            }
            while (true)
            {
                yield return null;
                var passTime = Time.time - st;
                var t = passTime % data.time;
                var f = t / data.time;
                var val = data.curve.Evaluate(f);
                onUpdate(val);
                if (!data.loop && passTime >= data.time)
                    break;
            }
        }

    }
}
